
\begingroup
\tikzset{external/figure name/.add={}{tol_}}%
    \tikzset{
%        font=\scriptsize,
        general matrix style/.style={
            font=\tiny,
            every matrix/.append style={
                matrix anchor=north,
                inner ysep=0pt,
%                fill=red!10,
                ampersand replacement=\&,
                matrix of nodes,
                nodes={
                    anchor=south,
                    minimum size=2mm,
                },
                row 1/.style={
                    inner sep=0pt,
                    text height=1.5ex,
                    text depth=.25ex,
                },
                column 1/.style={
                    inner ysep=0pt,
%                    anchor=base east,
                },
            },
        },
        matrix style/.style={
            general matrix style,
            every matrix/.append style={
                row 2 column 1/.style={
                    every node/.append style={
                        font=\bfseries\scriptsize,
                        draw,
                    },
                },
                row sep=0pt,
                column sep=0pt,
            },
        },
        matrix style 2/.style={
            general matrix style,
            every matrix/.append style={
                row 1 column 1/.style={
                    every node/.append style={
                        font=\bfseries\scriptsize,
                        draw,
                        text height=,
                        text depth=,
                    },
                },
                row sep=0pt,
                column sep=0pt,
            },
        },
        matrix style 3/.style={
            matrix style,
            every matrix/.append style={
                row 1/.append style={
                    every node/.style={
%                        draw=red,
                        text height=,
                        text depth=,
                        rotate=90,
                        anchor=west,
                    },
                },
                row sep=0pt,
                column sep=0pt,
            },
        },
    }


% =============================================================================
\NewDocumentCommand{\MATRIXqualOne}{mmO{0}}{%
%%%%%    \tikzset{/tikz/external/export=true}%
    \newcommand*\scheme{#2}%
    \pgfmanualpdflabel{colormap/\scheme-3}{}%
    \pgfmanualpdflabel{colormap/\scheme-4}{}%
    \pgfmanualpdflabel{colormap/\scheme-5}{}%
    \pgfmanualpdflabel{colormap/\scheme-6}{}%
    \pgfmanualpdflabel{colormap/\scheme-7}{}%
    \pgfmanualpdflabel{colormap/\scheme-8}{}%
    \ifnum#1>8%
        \pgfmanualpdflabel{colormap/\scheme-9}{}%
        \ifnum#1>9%
            \pgfmanualpdflabel{colormap/\scheme-10}{}%
            \pgfmanualpdflabel{colormap/\scheme-11}{}%
            \pgfmanualpdflabel{colormap/\scheme-12}{}%
        \fi%
    \fi%
    \pgfmanualpdflabel{colormap/\scheme}{}%
    \tikzsetnextfilename{tol-palette-#2}%
    \begin{tikzpicture}[
        baseline,
        matrix style,
    ]
        \newcommand*\classes{#1}
        % instantiate all colormaps for \scheme:
        \pgfplotsset{
            colormap/\scheme-3,
            colormap/\scheme-4,
            colormap/\scheme-5,
            colormap/\scheme-6,
            colormap/\scheme-7,
            colormap/\scheme-8,
        }
        \foreach \x in {A,...,H} {\pgfmanualpdflabel{\scheme-\x}{}}
        \ifnum\classes>8
        \foreach \x in {I} {\pgfmanualpdflabel{\scheme-\x}{}}
            \pgfplotsset{
                colormap/\scheme-9,
            }
        \fi
        \ifnum\classes=12
        \foreach \x in {J,...,L} {\pgfmanualpdflabel{\scheme-\x}{}}
            \pgfplotsset{
                colormap/\scheme-10,
                colormap/\scheme-11,
                colormap/\scheme-12,
            }
        \fi
            \pgfplotsset{
                colormap/\scheme,
            }%
            %
            \newcommand*\EmptyRows{#3}

            % directly access the correct color:
            % #1 is one of A,B,...,M
            \def\C##1{\node [fill=\scheme-##1] {};}
            %
            % indirectly access the correct color
            % #1 is an index into the colormap, 0,1,...,13
            % #2 is the number of colors in the colormap (qualifies which colormap to use)
            % This is redundant, but it allows us to verify that the colormaps
            % work as intended without typos:
            \newcommand*\CC[2]{\node [/pgfplots/index of colormap=##1 of \scheme-##2, fill] {};}
            %
            % define dummy node to just produce space
            \newcommand*\Dummy{\node {};}
        \ifnum\classes=7
            \matrix (m) {
                        \& A          \& B          \& C          \& D          \& E          \& F          \& G          \& H          \\
                \scheme \& \C{A}      \& \C{B}      \& \C{C}      \& \C{D}      \& \C{E}      \& \C{F}      \& \C{G}      \& \C{H}      \\
            };
        \else\ifnum\classes=12
            \matrix (m) {
                        \& A          \& B          \& C          \& D          \& E          \& F          \& G          \& H          \& I          \& J          \& K          \& L           \& M           \& [1ex] gap \\
                \scheme \& \C{A}      \& \C{B}      \& \C{C}      \& \C{D}      \& \C{E}      \& \C{F}      \& \C{G}      \& \C{H}      \& \C{I}      \& \C{J}      \& \C{K}      \& \C{L}       \& \C{M}       \& \C{gap}   \\
                3       \&            \& \CC{0}{3}  \&            \&            \&            \&            \&            \& \CC{1}{3}  \&            \& \CC{2}{3}  \&            \&             \&             \& \C{gap}   \\
                4       \&            \& \CC{0}{4}  \&            \&            \&            \& \CC{1}{4}  \&            \& \CC{2}{4}  \&            \& \CC{3}{4}  \&            \&             \&             \& \C{gap}   \\
                5       \& \CC{0}{5}  \&            \&            \& \CC{1}{5}  \&            \& \CC{2}{5}  \&            \& \CC{3}{5}  \&            \& \CC{4}{5}  \&            \&             \&             \& \C{gap}   \\
                6       \& \CC{0}{6}  \&            \&            \& \CC{1}{6}  \&            \& \CC{2}{6}  \&            \& \CC{3}{6}  \&            \& \CC{4}{6}  \&            \&             \& \CC{5}{6}   \& \C{gap}   \\
                7       \& \CC{0}{7}  \&            \&            \& \CC{1}{7}  \& \CC{2}{7}  \& \CC{3}{7}  \&            \& \CC{4}{7}  \&            \& \CC{5}{7}  \&            \&             \& \CC{6}{7}   \& \C{gap}   \\
                8       \& \CC{0}{8}  \&            \&            \& \CC{1}{8}  \& \CC{2}{8}  \& \CC{3}{8}  \& \CC{4}{8}  \& \CC{5}{8}  \&            \& \CC{6}{8}  \&            \&             \& \CC{7}{8}   \& \C{gap}   \\
                9       \& \CC{0}{9}  \&            \&            \& \CC{1}{9}  \& \CC{2}{9}  \& \CC{3}{9}  \& \CC{4}{9}  \& \CC{5}{9}  \&            \& \CC{6}{9}  \&            \& \CC{7}{9}   \& \CC{8}{9}   \& \C{gap}   \\
                10      \& \CC{0}{10} \&            \&            \& \CC{1}{10} \& \CC{2}{10} \& \CC{3}{10} \& \CC{4}{10} \& \CC{5}{10} \& \CC{6}{10} \& \CC{7}{10} \&            \& \CC{8}{10}  \& \CC{9}{10}  \& \C{gap}   \\
                11      \& \CC{0}{11} \&            \& \CC{1}{11} \& \CC{2}{11} \& \CC{3}{11} \& \CC{4}{11} \& \CC{5}{11} \& \CC{6}{11} \& \CC{7}{11} \& \CC{8}{11} \&            \& \CC{9}{11}  \& \CC{10}{11} \& \C{gap}   \\
                12      \& \CC{0}{12} \&            \& \CC{1}{12} \& \CC{2}{12} \& \CC{3}{12} \& \CC{4}{12} \& \CC{5}{12} \& \CC{6}{12} \& \CC{7}{12} \& \CC{8}{12} \& \CC{9}{12} \& \CC{10}{12} \& \CC{11}{12} \& \C{gap}   \\
%%%%%                CM      \& \CM        \&            \&            \&            \&            \&            \&            \&            \&            \&            \&            \&             \&             \&           \\
            };
        \fi\fi

        % insert space as fake empty rows
        \ifnum\EmptyRows>0
            \path node [below=\EmptyRows*2mm of m,anchor=south] {};
        \fi
    \end{tikzpicture}%
}

\NewDocumentCommand{\MATRIXqualTwo}{mO{0}}{%
%%%%%    \tikzset{/tikz/external/export=true}%
    \newcommand*\scheme{#1}%
    \pgfmanualpdflabel{colormap/\scheme}{}%
    \tikzsetnextfilename{tol-palette-#1}%
    \begin{tikzpicture}[
        baseline,
        matrix style 2,
    ]
            \pgfplotsset{
                colormap/\scheme,
            }%
            %
            \newcommand*\EmptyRows{#2}

            % directly access the correct color:
            % #1 is one of A,B,...,M
            \def\C##1{\node [fill=\scheme-##1] {};}
            \def\D##1{\node [fill=\scheme light-##1] {};}
            \def\E##1{\node [fill=\scheme dark-##1] {};}
        \matrix (m) {
            \scheme \dots \& A     \& B     \& C     \& D     \& E     \& F     \& G     \\
            \scheme light \& \D{A} \& \D{B} \& \D{C} \& \D{D} \& \D{E} \& \D{F} \& \D{G} \\
            \scheme       \& \C{A} \& \C{B} \& \C{C} \& \C{D} \& \C{E} \& \C{F} \& \C{G} \\
            \scheme dark  \& \E{A} \& \E{B} \& \E{C} \& \E{D} \& \E{E} \& \E{F} \& \E{G} \\
        };

        % insert space as fake empty rows
        \ifnum\EmptyRows>0
            \path node [below=\EmptyRows*2mm of m,anchor=south] {};
        \fi
    \end{tikzpicture}%
}

\NewDocumentCommand{\MATRIXqualThree}{mmO{0}}{%
%%%%%    \tikzset{/tikz/external/export=true}%
    \newcommand*\scheme{#2}%
    \pgfmanualpdflabel{colormap/\scheme-4-}{}%
    \pgfmanualpdflabel{colormap/\scheme-5+}{}%
    \pgfmanualpdflabel{colormap/\scheme}{}%
    \tikzsetnextfilename{tol-palette-#2}%
    \begin{tikzpicture}[
        baseline,
        matrix style 3,
    ]
        \newcommand*\classes{#1}
        % instantiate all colormaps for \scheme:
        \pgfplotsset{
            colormap/\scheme-4-,
            colormap/\scheme-5+,
        }
        \foreach \x in {
            blue,
            cyan,
            green,
            yellow,
            red,
            pink,
            gray%
        } {\pgfmanualpdflabel{\scheme-\x}{}}

            % directly access the correct color:
            % #1 is one of A,B,...,M
            \def\C##1{\node [fill=\scheme-##1] {};}
            %
            % indirectly access the correct color
            % #1 is an index into the colormap, 0,1,...,13
            % #2 is the number of colors in the colormap (qualifies which colormap to use)
            % This is redundant, but it allows us to verify that the colormaps
            % work as intended without typos:
            \newcommand*\CC[2]{%
                \node [
                    /pgfplots/index of colormap=##1 of \scheme-##2,
                    fill,text=black,
                    label={[inner sep=0pt]center:##1},
                ] {};
            }
            %
            % define dummy node to just produce space
            \newcommand*\Dummy{\node {};}
            \matrix (m) {
                        \& blue       \& cyan       \& green      \& yellow     \& red        \& pink      \& [1ex] gray \\
                \scheme \& \C{blue}   \& \C{cyan}   \& \C{green}  \& \C{yellow} \& \C{red}    \& \C{pink}  \& \C{gray}   \\
                $4-$      \& \CC{0}{4-} \&            \& \CC{2}{4-} \& \CC{3}{4-} \& \CC{1}{4-} \&            \& \C{gray}   \\
                $5+$      \& \CC{0}{5+} \& \CC{1}{5+} \& \CC{2}{5+} \& \CC{3}{5+} \& \CC{4}{5+} \& \CC{5}{5+} \& \C{gray}   \\
%%%%%                CM      \& \CM        \&            \&            \&            \&            \&            \&            \&            \&            \&            \&            \&             \&             \&           \\
            };

        % insert space as fake empty rows
            \newcommand*\EmptyRows{#3}
        \ifnum\EmptyRows>0
            \path node [below=\EmptyRows*2mm of m,anchor=south] {};
        \fi
    \end{tikzpicture}%
}

\NewDocumentCommand{\MATRIXseq}{smO{0}}{%
%%%%%    \tikzset{/tikz/external/export=true}%
    \newcommand*\scheme{#2}%
    \pgfmanualpdflabel{colormap/\scheme-3}{}%
    \pgfmanualpdflabel{colormap/\scheme-4}{}%
    \pgfmanualpdflabel{colormap/\scheme-5}{}%
    \pgfmanualpdflabel{colormap/\scheme-6}{}%
    \pgfmanualpdflabel{colormap/\scheme-7}{}%
    \pgfmanualpdflabel{colormap/\scheme-8}{}%
    \pgfmanualpdflabel{colormap/\scheme-9}{}%
%%    \pgfmanualpdflabel{colormap/\scheme}{}%
    \foreach \x in {A,...,M} {\pgfmanualpdflabel{\scheme-\x}{}}%
    \tikzsetnextfilename{tol-seq-#2}%
    \begin{tikzpicture}[
        matrix style,
    ]
        % instantiate all colormaps for \scheme:
        \pgfplotsset{
            colormap/\scheme-3,
            colormap/\scheme-4,
            colormap/\scheme-5,
            colormap/\scheme-6,
            colormap/\scheme-7,
            colormap/\scheme-8,
            colormap/\scheme-9,
%%            colormap/\scheme,
        }%
        %
        % directly access the correct color:
        % #1 is one of A,B,...,M
        \def\C##1{\node [fill=\scheme-##1] {};}%
        % in case the color should be white, also draw a black frame
        \def\D##1{\node [draw=black,fill=\scheme-##1] {};}%
        %
        % indirectly access the correct color
        % #1 is an index into the colormap, 0,1,....,13
        % #2 is the number of colors in the colormap (qualifies which colormap to use)
        % This is redundant, but it allows us to verify that the colormaps
        % work as intended without typos:
        \newcommand*\CC[2]{\node [/pgfplots/index of colormap=##1 of \scheme-##2, fill] {};}%
        % in case the color should be white, also draw a black frame
        \newcommand*\DD[2]{\node [/pgfplots/index of colormap=##1 of \scheme-##2, fill, draw=black] {};}%
        %
        % define a colorbar that has the same size as a row of colors in the matrix
        \newcommand*\CM{%
            \node[inner sep=0pt,xshift=-3.5pt,overlay,anchor=south west]{%
    \IfBooleanTF{#1}{%
                \pgfmathparse{14*2mm + 16*\pgflinewidth + 1.0pt}%
    }{%
                \pgfmathparse{14*2mm + 15*\pgflinewidth + 1.1pt}%
    }
                \let\width=\pgfmathresult
                \pgfplotscolormaptoshadingspec{\scheme}{\width pt}\result
                \def\tempb{\pgfdeclarehorizontalshading{tempshading}{2mm}}%
                \expandafter\tempb\expandafter{\result}%
                \pgfuseshading{tempshading}%
            };
        }%
        \matrix (m) {
    \IfBooleanTF{#1}{%
                    \& A         \& B         \& C         \& D         \& E         \& F         \& G         \& H         \& I         \& J         \& K         \& L         \& M         \\
            \scheme \& \D{A}     \& \C{B}     \& \C{C}     \& \C{D}     \& \C{E}     \& \C{F}     \& \C{G}     \& \C{H}     \& \C{I}     \& \C{J}     \& \C{K}     \& \C{L}     \& \C{M}     \\
            3       \&           \&           \& \CC{0}{3} \&           \&           \& \CC{1}{3} \&           \&           \& \CC{2}{3} \&           \&           \&           \&           \\
            4       \&           \& \CC{0}{4} \&           \&           \& \CC{1}{4} \&           \& \CC{2}{4} \&           \&           \& \CC{3}{4} \&           \&           \&           \\
            5       \&           \& \CC{0}{5} \&           \&           \& \CC{1}{5} \&           \& \CC{2}{5} \&           \& \CC{3}{5} \&           \& \CC{4}{5} \&           \&           \\
            6       \&           \& \CC{0}{6} \&           \& \CC{1}{6} \&           \& \CC{2}{6} \& \CC{3}{6} \&           \& \CC{4}{6} \&           \& \CC{5}{6} \&           \&           \\
            7       \&           \& \CC{0}{7} \&           \& \CC{1}{7} \&           \& \CC{2}{7} \& \CC{3}{7} \& \CC{4}{7} \&           \& \CC{5}{7} \&           \& \CC{6}{7} \&           \\
            8       \& \DD{0}{8} \&           \& \CC{1}{8} \& \CC{2}{8} \&           \& \CC{3}{8} \& \CC{4}{8} \& \CC{5}{8} \&           \& \CC{6}{8} \&           \& \CC{7}{8} \&           \\
            9       \& \DD{0}{9} \&           \& \CC{1}{9} \& \CC{2}{9} \&           \& \CC{3}{9} \& \CC{4}{9} \& \CC{5}{9} \&           \& \CC{6}{9} \& \CC{7}{9} \&           \& \CC{8}{9} \\
%%            CM      \& \CM       \&           \&           \&           \&           \&           \&           \&           \&           \&           \&           \&           \&           \\
    }{%
                    \& A         \& B         \& C         \& D         \& E         \& F         \& G         \& H         \& I         \& J         \& K         \& L         \& M         \\
            \scheme \& \C{A}     \& \C{B}     \& \C{C}     \& \C{D}     \& \C{E}     \& \C{F}     \& \C{G}     \& \C{H}     \& \C{I}     \& \C{J}     \& \C{K}     \& \C{L}     \& \C{M}     \\
            3       \&           \&           \& \CC{0}{3} \&           \&           \& \CC{1}{3} \&           \&           \& \CC{2}{3} \&           \&           \&           \&           \\
            4       \&           \& \CC{0}{4} \&           \&           \& \CC{1}{4} \&           \& \CC{2}{4} \&           \&           \& \CC{3}{4} \&           \&           \&           \\
            5       \&           \& \CC{0}{5} \&           \&           \& \CC{1}{5} \&           \& \CC{2}{5} \&           \& \CC{3}{5} \&           \& \CC{4}{5} \&           \&           \\
            6       \&           \& \CC{0}{6} \&           \& \CC{1}{6} \&           \& \CC{2}{6} \& \CC{3}{6} \&           \& \CC{4}{6} \&           \& \CC{5}{6} \&           \&           \\
            7       \&           \& \CC{0}{7} \&           \& \CC{1}{7} \&           \& \CC{2}{7} \& \CC{3}{7} \& \CC{4}{7} \&           \& \CC{5}{7} \&           \& \CC{6}{7} \&           \\
            8       \& \CC{0}{8} \&           \& \CC{1}{8} \& \CC{2}{8} \&           \& \CC{3}{8} \& \CC{4}{8} \& \CC{5}{8} \&           \& \CC{6}{8} \&           \& \CC{7}{8} \&           \\
            9       \& \CC{0}{9} \&           \& \CC{1}{9} \& \CC{2}{9} \&           \& \CC{3}{9} \& \CC{4}{9} \& \CC{5}{9} \&           \& \CC{6}{9} \& \CC{7}{9} \&           \& \CC{8}{9} \\
%%            CM      \& \CM       \&           \&           \&           \&           \&           \&           \&           \&           \&           \&           \&           \&           \\
    }
        };

        % insert space as fake empty rows
            \newcommand*\EmptyRows{#3}
        \ifnum\EmptyRows>0
            \path node [below=\EmptyRows*2mm of m,anchor=south] {};
        \fi
    \end{tikzpicture}%
}

\NewDocumentCommand{\MATRIXdiv}{m}{%
%%%%%    \tikzset{/tikz/external/export=true}%
    \newcommand*\scheme{#1}%
    \pgfmanualpdflabel{colormap/\scheme-3}{}%
    \pgfmanualpdflabel{colormap/\scheme-4}{}%
    \pgfmanualpdflabel{colormap/\scheme-5}{}%
    \pgfmanualpdflabel{colormap/\scheme-6}{}%
    \pgfmanualpdflabel{colormap/\scheme-7}{}%
    \pgfmanualpdflabel{colormap/\scheme-8}{}%
    \pgfmanualpdflabel{colormap/\scheme-9}{}%
    \pgfmanualpdflabel{colormap/\scheme-10}{}%
    \pgfmanualpdflabel{colormap/\scheme-11}{}%
%%    \pgfmanualpdflabel{colormap/\scheme}{}%
    \foreach \x in {A,...,O} {\pgfmanualpdflabel{\scheme-\x}{}}%
    \tikzsetnextfilename{tol-div-#1}%
    \begin{tikzpicture}[
        matrix style,
    ]
        % instantiate all colormaps for \scheme:
        \pgfplotsset{
            colormap/\scheme-3,
            colormap/\scheme-4,
            colormap/\scheme-5,
            colormap/\scheme-6,
            colormap/\scheme-7,
            colormap/\scheme-8,
            colormap/\scheme-9,
            colormap/\scheme-10,
            colormap/\scheme-11,
%%            colormap/\scheme,
        }%
        %
        % directly access the correct color:
        % #1 is one of A,B,...,M
        \def\C##1{\node [fill=\scheme-##1] {};}%
        % in case the color should be white, also draw a black frame
        \def\D##1{\node [draw=black,fill=\scheme-##1] {};}%
        %
        % indirectly access the correct color
        % #1 is an index into the colormap, 0,1,...,13
        % #2 is the number of colors in the colormap (qualifies which colormap to use)
        % This is redundant, but it allows us to verify that the colormaps
        % work as intended without typos:
        \newcommand*\CC[2]{\node [/pgfplots/index of colormap=##1 of \scheme-##2, fill] {};}%
        % in case the color should be white, also draw a black frame
        \newcommand*\DD[2]{\node [/pgfplots/index of colormap=##1 of \scheme-##2, fill, draw=black] {};}%
        %
        % define a colorbar that has the same size as a row of colors in the matrix
        \newcommand*\CM{%
            \node[inner sep=0pt,xshift=-3.5pt,overlay,anchor=south west]{%
    \IfBooleanTF{#1}{%
                \pgfmathparse{16*2mm + 18*\pgflinewidth + 2.3pt}%
    }{%
                \pgfmathparse{16*2mm + 17*\pgflinewidth + 2.3pt}%
    }%
                \let\width=\pgfmathresult
                \pgfplotscolormaptoshadingspec{\scheme}{\width pt}\result
                \def\tempb{\pgfdeclarehorizontalshading{tempshading}{2mm}}%
                \expandafter\tempb\expandafter{\result}%
                \pgfuseshading{tempshading}%
            };
        }%
        \matrix {
    \IfBooleanTF{#1}{%
                    \& A          \& B          \& C         \& D          \& E         \& F          \& G          \& H          \& I          \& J          \& K         \& L          \& M         \& N          \& O           \\
            \scheme \& \C{A}      \& \C{B}      \& \C{C}     \& \C{D}      \& \C{E}     \& \C{F}      \& \C{G}      \& \D{H}      \& \C{I}      \& \C{J}      \& \C{K}     \& \C{L}      \& \C{M}     \& \C{N}      \& \C{O}       \\
            3       \&            \&            \&           \&            \& \CC{0}{3} \&            \&            \& \DD{1}{3}  \&            \&            \& \CC{2}{3} \&            \&           \&            \&             \\
            4       \&            \&            \& \CC{0}{4} \&            \&           \& \CC{1}{4}  \&            \&            \&            \& \CC{2}{4}  \&           \&            \& \CC{3}{4} \&            \&             \\
            5       \&            \&            \& \CC{0}{5} \&            \&           \& \CC{1}{5}  \&            \& \DD{2}{5}  \&            \& \CC{3}{5}  \&           \&            \& \CC{4}{5} \&            \&             \\
            6       \&            \& \CC{0}{6}  \&           \&            \& \CC{1}{7} \&            \& \CC{2}{6}  \&            \& \CC{3}{6}  \&            \& \CC{4}{6} \&            \&           \& \CC{5}{6}  \&             \\
            7       \&            \& \CC{0}{7}  \&           \&            \& \CC{1}{7} \&            \& \CC{2}{7}  \& \DD{3}{7}  \& \CC{4}{7}  \&            \& \CC{5}{7} \&            \&           \& \CC{6}{7}  \&             \\
            8       \&            \& \CC{0}{8}  \&           \& \CC{1}{8}  \&           \& \CC{2}{8}  \& \CC{3}{8}  \&            \& \CC{4}{8}  \& \CC{5}{8}  \&           \& \CC{6}{8}  \&           \& \CC{7}{8}  \&             \\
            9       \&            \& \CC{0}{9}  \&           \& \CC{1}{9}  \&           \& \CC{2}{9}  \& \CC{3}{9}  \& \DD{4}{9}  \& \CC{5}{9}  \& \CC{6}{9}  \&           \& \CC{7}{9}  \&           \& \CC{8}{9}  \&             \\
            10      \& \CC{0}{10} \& \CC{1}{10} \&           \& \CC{2}{10} \&           \& \CC{3}{10} \& \CC{4}{10} \&            \& \CC{5}{10} \& \CC{6}{10} \&           \& \CC{7}{10} \&           \& \CC{8}{10} \& \CC{9}{10}  \\
            11      \& \CC{0}{11} \& \CC{1}{11} \&           \& \CC{2}{11} \&           \& \CC{3}{11} \& \CC{4}{11} \& \DD{5}{11} \& \CC{6}{11} \& \CC{7}{11} \&           \& \CC{8}{11} \&           \& \CC{9}{11} \& \CC{10}{11} \\
%%            CM      \& \CM        \&            \&           \&            \&           \&            \&            \&            \&            \&            \&           \&            \&           \&            \&             \\
    }{%
                    \& A          \& B          \& C         \& D          \& E         \& F          \& G          \& H          \& I          \& J          \& K         \& L          \& M         \& N          \& O           \\
            \scheme \& \C{A}      \& \C{B}      \& \C{C}     \& \C{D}      \& \C{E}     \& \C{F}      \& \C{G}      \& \C{H}      \& \C{I}      \& \C{J}      \& \C{K}     \& \C{L}      \& \C{M}     \& \C{N}      \& \C{O}       \\
            3       \&            \&            \&           \&            \& \CC{0}{3} \&            \&            \& \CC{1}{3}  \&            \&            \& \CC{2}{3} \&            \&           \&            \&             \\
            4       \&            \&            \& \CC{0}{4} \&            \&           \& \CC{1}{4}  \&            \&            \&            \& \CC{2}{4}  \&           \&            \& \CC{3}{4} \&            \&             \\
            5       \&            \&            \& \CC{0}{5} \&            \&           \& \CC{1}{5}  \&            \& \CC{2}{5}  \&            \& \CC{3}{5}  \&           \&            \& \CC{4}{5} \&            \&             \\
            6       \&            \& \CC{0}{6}  \&           \&            \& \CC{1}{7} \&            \& \CC{2}{6}  \&            \& \CC{3}{6}  \&            \& \CC{4}{6} \&            \&           \& \CC{5}{6}  \&             \\
            7       \&            \& \CC{0}{7}  \&           \&            \& \CC{1}{7} \&            \& \CC{2}{7}  \& \CC{3}{7}  \& \CC{4}{7}  \&            \& \CC{5}{7} \&            \&           \& \CC{6}{7}  \&             \\
            8       \&            \& \CC{0}{8}  \&           \& \CC{1}{8}  \&           \& \CC{2}{8}  \& \CC{3}{8}  \&            \& \CC{4}{8}  \& \CC{5}{8}  \&           \& \CC{6}{8}  \&           \& \CC{7}{8}  \&             \\
            9       \&            \& \CC{0}{9}  \&           \& \CC{1}{9}  \&           \& \CC{2}{9}  \& \CC{3}{9}  \& \CC{4}{9}  \& \CC{5}{9}  \& \CC{6}{9}  \&           \& \CC{7}{9}  \&           \& \CC{8}{9}  \&             \\
            10      \& \CC{0}{10} \& \CC{1}{10} \&           \& \CC{2}{10} \&           \& \CC{3}{10} \& \CC{4}{10} \&            \& \CC{5}{10} \& \CC{6}{10} \&           \& \CC{7}{10} \&           \& \CC{8}{10} \& \CC{9}{10}  \\
            11      \& \CC{0}{11} \& \CC{1}{11} \&           \& \CC{2}{11} \&           \& \CC{3}{11} \& \CC{4}{11} \& \CC{5}{11} \& \CC{6}{11} \& \CC{7}{11} \&           \& \CC{8}{11} \&           \& \CC{9}{11} \& \CC{10}{11} \\
%%            CM      \& \CM        \&            \&           \&            \&           \&            \&            \&            \&            \&            \&           \&            \&           \&            \&             \\
    }
        };
    \end{tikzpicture}%
}

% =============================================================================

\section{ColorTol}
\def\pgfplotsmanualcurlibrary{colormaps}

\emph{An extension by Paul Tol and Stefan Pinnow}

\begin{pgfplotslibrary}{colortol}

This library brings the color schemes/palettes created by Paul Tol published at
\url{https://personal.sron.nl/~pault/} to PGFPlots. The main color palettes are
designed so the colors are
%
\begin{itemize}
    \item distinct to all people, including color-blind readers,
    \item distinct from black and white,
    \item distinct on screen and paper, and
    \item still match well together.
\end{itemize}
%
They are meant for qualitative data, where magnitude differences are not
relevant; this includes text and lines. There are also some color schemes for
ordered data (sequential or diverging) which are similar to ColorBrewer ones
(see the corresponding library in Section~\ref{sec:colorbrewer}).

They can consist of up to 12 different data classes, i.e.\@ colors, per scheme
and are provided as color maps as well as as cycle lists.


\noindent
\begin{tabular}{rrr}
    \MATRIXqualOne{12}{colortol-P1}
        & \MATRIXqualTwo{colortol-P2}
            & \MATRIXqualThree{8}{colortol-P3} \\
\end{tabular}

\noindent
\begin{tabular}{rr}
    \MATRIXseq{colortol-seq}[2] & \MATRIXdiv{colortol-div} \\
\end{tabular}


% =============================================================================

%\subsection{Usage}
%
%In the following the available schemes are presented in graphical form as ``swatches''.
%
%\subsubsection*{Color Schemes as ``Swatches''}
%\label{sec:pgfplots:brewer:usage}
%A swatch is a matrix showing all available colors for a specific scheme, and the available color compilations.
%
%{\centering
%% inspired from http://mkweb.bcgsc.ca/brewer/swatches/brewer-palettes-swatches.pdf
%\tikzset{/tikz/external/export=true}%
%\tikzsetnextfilename{brewer-fig-intro-GnBu}%
%\begin{tikzpicture}[
%    font=\scriptsize,
%]
%    \newcommand*\scheme{GnBu}
%    % instantiate all colormaps for \scheme:
%    \pgfplotsset{
%        colormap/\scheme-3,
%        colormap/\scheme-4,
%        colormap/\scheme-5,
%        colormap/\scheme-6,
%        colormap/\scheme-7,
%        colormap/\scheme-8,
%        colormap/\scheme-9,
%        colormap/\scheme,
%    }%
%
%    % directly access the correct color:
%    % #1 is one of A,B,...,M
%    \def\C#1{\node [fill=\scheme-#1] {};}
%
%    % just set a label to an invisible cell
%    \def\D#1{\node [draw=none] (#1) {};}
%
%    % define a colorbar that has the same size as a row of colors in the matrix
%    \newcommand*\CM{%
%        \node[inner sep=0pt,xshift=-4.4pt,overlay,anchor=south west]{%
%            \pgfmathparse{13*3mm + 0pt}%
%            \let\width=\pgfmathresult
%            \pgfplotscolormaptoshadingspec{\scheme}{\width pt}\result
%            \def\tempb{\pgfdeclarehorizontalshading{tempshading}{3mm}}%
%            \expandafter\tempb\expandafter{\result}%
%            \pgfuseshading{tempshading}%
%        };
%    }
%
%    \matrix[
%        ampersand replacement=\&,
%        matrix of nodes,
%        nodes={
%            anchor=south,
%            minimum size=3mm,
%        },
%        row 1/.style={
%            inner sep=0pt,
%        },
%        column 1/.style={
%            inner ysep=0pt,
%            anchor=base east,
%        },
%        row 2 column 1/.style={
%            every node/.append style={
%                font=\bfseries\scriptsize,
%                draw,
%            },
%        },
%        row sep=0pt,
%        column sep=0pt,
%    ] (m) {
%                \& A     \& B     \& C     \& D     \& E     \& F     \& G     \& H     \& I     \& J     \& K     \& L     \& M     \\
%        \scheme \& \C{A} \& \C{B} \& \C{C} \& \C{D} \& \C{E} \& \C{F} \& \C{G} \& \C{H} \& \C{I} \& \C{J} \& \C{K} \& \C{L} \& \node [fill=\scheme-M] (c) {}; \\
%        3       \&       \&       \& \C{C} \&       \&       \& \C{F} \&       \&       \& \C{I} \&       \&       \&       \&       \\
%        4       \&       \& \C{B} \&       \&       \& \C{E} \&       \& \C{G} \&       \&       \& \C{J} \&       \&       \&       \\
%        5       \&       \& \C{B} \&       \&       \& \C{E} \&       \& \C{G} \&       \& \C{I} \&       \& \C{K} \&       \&       \\
%        6       \&       \& \C{B} \&       \& \C{D} \&       \& \C{F} \& \C{G} \&       \& \C{I} \&       \& \C{K} \&       \&       \\
%        7       \& \D{a} \& \C{B} \&       \& \C{D} \&       \& \C{F} \& \C{G} \& \C{H} \&       \& \C{J} \&       \& \C{L} \& \D{b} \\
%        8       \& \C{A} \&       \& \C{C} \& \C{D} \&       \& \C{F} \& \C{G} \& \C{H} \&       \& \C{J} \&       \& \C{L} \&       \\
%        9       \& \C{A} \&       \& \C{C} \& \C{D} \&       \& \C{F} \& \C{G} \& \C{H} \&       \& \C{J} \& \C{K} \&       \& \C{M} \\
%        CM      \& \CM   \&       \&       \&       \&       \&       \&       \&       \&       \&       \&       \&       \& \D{CM} \\
%    };
%
%    \node [coordinate,pin={[align=center]above left:(short) \\ scheme \\ name}]
%        at ([xshift=5pt] m-2-1.north west) {};
%    \node [coordinate,pin={right:letters to create scheme color name}]
%        at ([xshift=5pt] m-1-14) {};
%    \node [coordinate,pin={right:all colors of scheme}]
%        at ([xshift=5pt] c) {};
%    \node [coordinate,pin={[align=center]below:numbers to create \\ (full) scheme name \\ (number of data classes)}]
%        at ([xshift=0pt] m-10-1.south) {};
%    \node [coordinate,pin={[align=left]right:colormap based on \\ previous row colors; \\
%                                             (also) accessible by the \\ short scheme name}]
%        at ([xshift=5pt] CM) {};
%
%    \draw [red,rounded corners=2pt]
%        ([xshift=-1pt] a.north west)
%            rectangle
%        ([xshift=+1pt] b.south east)
%    ;
%    \node [coordinate,pin=right:colors of scheme \texttt{GnBu-7}]
%        at ([xshift=1pt] b.east) {};
%
%\end{tikzpicture}
%
%}%
%
%Such swatches are read as follows:
%\begin{enumerate}
%    \item At the top left of the block you find the (short) name of the scheme.
%    \item The following color blocks are the colors the scheme consists of.
%    \item To get the (full) color name, combine the (short) scheme name with
%        a hyphen and a letter of the first row, e.g.
%
%        |\tikz \fill[color=GnBu-H] (0,0) rectangle (1em,2ex);|
%
%        which results in       \tikz \fill[color=GnBu-H] (0,0) rectangle (1em,2ex);.
%    \item To get the full scheme name, which can be used as colormap or cycle
%        list, combine the (short) scheme name with a hyphen and a number of
%        the first column, e.g. |cycle list name=GnBu-7|. \\
%        The special case of using the (short) scheme name only is also
%        provided and is an alias for the full scheme name with the highest
%        number, e.g. scheme name \texttt{GnBu} equals \texttt{GnBu-9}.
%    \item The last row shows a continuous color map based on the previous row,
%        that is in the example \texttt{GnBu-9}.
%    \item The rest of the matrix shows the colors used in the corresponding
%        scheme.
%\end{enumerate}
%
%
%\subsubsection*{Activating Color Schemes}
%
%In order to activate a |colorbrewer| |colormap|, say, |BuGn-5|, you have to use the key
%
%  |colormap/BuGn-5|.
%
%\noindent This will initialize and select the associated |colormap|. It will also initialize the associated |cycle list| (but will not select it).
%%
%In order to initialize and select the |cycle list| of name |BuGn-5|, you have to use the key
%
%    |cycle list/BuGn-5|.
%
%\noindent This will initialize and select the associated |cycle list|. It will also initialize (but not select) the associated |colormap|.
%
%
%Note that |cycle list|s shipped with |colorbrewer| merely consist of \emph{colors}. However, a good |cycle list| typically also comes with markers and perhaps line style variations. In order to combine a pure color-based |cycle list| with markers, you should make use of the features |cycle multi list|, |cycle multiindex list|, and
%|cycle multiindex* list|, for example using
%\begin{codeexample}[code only]
%\pgfplotsset{
%    % initialize Set1-5:
%    cycle list/Set1-5,
%    % combine it with 'mark list*':
%    cycle multiindex* list={
%        mark list*\nextlist
%        Set1-5\nextlist
%    },
%}
%\end{codeexample}
%\noindent Please refer to the reference manual for |cycle multiindex* list| for details.
%
%
%\subsection{Sequential Schemes}
%    Sequential schemes are useful for ordered data progressing low to high.
%
%\noindent
%\begin{tabular}{rrr}
%    \MATRIXseq{BuGn}   & \MATRIXseq{PuRd}   & \MATRIXseq{Blues}   \\
%    \MATRIXseq{BuPu}   & \MATRIXseq{RdPu}   & \MATRIXseq{Greens}  \\
%    \MATRIXseq{GnBu}   & \MATRIXseq{YlGn}   & \MATRIXseq*{Greys}  \\
%    \MATRIXseq{OrRd}   & \MATRIXseq{YlGnBu} & \MATRIXseq{Oranges} \\
%    \MATRIXseq{PuBu}   & \MATRIXseq{YlOrBr} & \MATRIXseq{Purples} \\
%    \MATRIXseq{PuBuGn} & \MATRIXseq{YlOrRd} & \MATRIXseq{Reds}    \\
%\end{tabular}
%
%
%\subsection{Diverging Schemes}
%    Diverging schemes highlight changes from some mean value.
%
%\noindent
%\begin{tabular}{rrr}
%    \MATRIXdiv{BrBG}   & \MATRIXdiv*{RdGy}   & \MATRIXdiv{RdYlBu} \\
%    \MATRIXdiv{PiYG}   & \MATRIXdiv{PuOr}  & \MATRIXdiv{RdYlGn}\\
%    \MATRIXdiv{PRGn}   & \MATRIXdiv{RdBu}  & \MATRIXdiv{Spectral}\\
%\end{tabular}
%
%    Note that you can adopt |point meta min| and |point meta max| such that the |colormap|'s mean value fits the data (for example by forcing |point meta min=-2| and |point meta max=+2|).
%
%
%\subsection{Qualitative Schemes}
%    Qualitative schemes are useful if colors have no special order, but should be distinguishable.
%
%\noindent
%\begin{tabular}{rrr}
%    \MATRIXqual{8}{Accent}     & \MATRIXqual{9}{Pastel1}[3] & \MATRIXqual{8}{Pastel2} \\
%    \MATRIXqual{8}{Dark2}[1]   & \MATRIXqual{9}{Set1}    & \MATRIXqual{8}{Set2}[4]\\
%    \MATRIXqual{12}{Paired}    & \MATRIXqual{12}{Set3} & \\
%\end{tabular}
%
%
%
%\subsection{Interaction with the ColorBrewer website}
%
%To find a scheme, e.g. the above chosen \texttt{GnBu-7} on
%\url{http://colorbrewer2.org/}
%%
%\begin{itemize}
%    \item first select the ``Nature of your data'' type; in this case
%        ``sequential'',
%    \item then select the ``Number of data classes'', which is 7,
%    \item and last select the corresponding scheme in the ``Pick a color
%        scheme'' section. (This is a bit tricky, because you can only see the
%        name at the top of the lower right corner, where all the colors are
%        listed. When you have selected the right one you should read there
%        ``7-class GnBu''. But perhaps it helps when you know that ``Gn''
%        stands for green and ``Bu'' for blue, so you are searching for a
%        scheme going from green to blue, which in this case is the third
%        color scheme in the first row of ``Multi-hue''.)
%    \item The color names, e.g. \texttt{GnBu-H}, cannot be extracted from the
%        page directly though, because you cannot simply count to the number
%        and replace it with the corresponding letter. This intentionally was
%        avoided, because then one would define the same color multiple times
%        with different names, e.g. \texttt{GnBu-H} 3 times.
%    \item If you should need let's say the 5th color of the scheme
%        \texttt{GnBu-7} you don't have to trial and error or have a look at
%        the manual. %
%        \pgfplotsset{colormap/GnBu-7}%
%        You can simply extract this color from the corresponding
%        color map via the \verb|index of colormap| feature, e.g.
%
%        |\tikz \fill[index of colormap={4 of GnBu-7}] (0,0) rectangle (1em,2ex);|
%
%        which results in \tikz \fill[index of colormap={4 of GnBu-7}] (0,0)
%        rectangle (1em,2ex);. Note that the index is $4$ instead of $5$ because the index starts
%        with $0$. Note furthermore that you have to invoke |\pgfplotsset{colormap/GnBu-7}| before using this key.
%\end{itemize}
%
%
%
%\subsection{External Examples}
%
%If you want to see some examples, where Brewer has used her schemes, have a
%look at \url{https://www.census.gov/population/www/cen2000/atlas/index.html}.

\end{pgfplotslibrary}
%
%\begin{tikzlibrary}{colorbrewer}
%    A library which contains just the color definitions like |GnBu-B|. Please refer to Section~\ref{sec:pgfplots:brewer:usage} for a list of available colors.
%\end{tikzlibrary}
\endgroup
\endinput
